//
//  YCExamBaseTableView.m
//  YCSchool
//
//  Created by Kylin on 16/5/2.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  基础的tableView ,所有的科目都是使用该tableView进行展示的.只是传过来的cell不一样

#import "YCExamBaseTableView.h"
#import "YCExamStatusCell.h"
#import "YCExamStrategyCell.h"
#import "YCTitleCell.h"
#import "YCTeachVideoCell.h"
#import "YCSpecialTrainingCell.h"

// cell 里面CollectionViewCell弹出的控制器
#import "YCExamRuleIntroduceViewController.h"
#import "YCExamRecordViewController.h"
#import "YCResultRankViewController.h"
#import "YCStudyDriveStrategyViewController.h"


@interface YCExamBaseTableView()<UITableViewDelegate,UITableViewDataSource,YCStrategyCellDelegate>
/**talbeView*/
@property(nonatomic,weak)UITableView *tableView;
/**titleCell*/
@property(nonatomic,strong)NSString *titleCell;
/**攻略的cell*/
@property(nonatomic,strong)NSString *strategyCell;
/**考试状态的cell*/
@property(nonatomic,strong)NSString *statusCell;
/**视频教学的Cell */
@property(nonatomic,strong)NSString *teachVideoCell;
/**专项训练的cell*/
@property(nonatomic,strong)NSString *specialCell;

/**控制器数组*/
@property(nonatomic,strong)NSMutableArray *VCArray;
@end


static NSString *const ExamStatusCell = @"ExamStatusCell";
static NSString *const ExamStrategyCell = @"ExamStrategyCell";
static NSString *const TitleCell = @"TitleCell";
static NSString *const TeachVideoCell = @"TeachVideoCell";
static NSString *const SpcialTrainingCell = @"SpcialTrainingCell";
@implementation YCExamBaseTableView


#pragma mark- 懒加载类的对应的字符串
- (NSString *)titleCell{
    if (!_titleCell) {
        _titleCell = NSStringFromClass([YCTitleCell class]);
    }
    return _titleCell;
}

- (NSString *)strategyCell{
    if (!_strategyCell) {
        _strategyCell = NSStringFromClass([YCExamStrategyCell class]);
    }
    return _strategyCell;
}

- (NSString *)statusCell{
    if (!_statusCell) {
        _statusCell = NSStringFromClass([YCExamStatusCell class]);
    }
    return _statusCell;
}

- (NSString *)teachVideoCell{
    if (!_teachVideoCell) {
        _teachVideoCell = NSStringFromClass([YCTeachVideoCell class]);
    }
    
    return _teachVideoCell;
}

- (NSString *)specialCell{
    if (!_specialCell) {
        _specialCell = NSStringFromClass([YCSpecialTrainingCell class]);
    }
    return _specialCell;
}
- (void)dealloc{
    YCLog(@"我死了---");
}

// 学车攻略cell弹出的控制器的数组,根据index取出相应的控制器
-(NSMutableArray *)VCArray{
    if (!_VCArray) {
        NSArray *arr = @[[[YCExamRuleIntroduceViewController alloc]init],
                         [[YCExamRecordViewController alloc]init],
                         [[YCResultRankViewController alloc]init],
                         [[YCStudyDriveStrategyViewController alloc]init]];
        _VCArray = [NSMutableArray arrayWithArray:arr];
    }
    return _VCArray;
}


// 重写构造方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self tableView];
    }
    
    return self;
}

// 添加一个tableView
- (UITableView *)tableView{
    if (!_tableView) {
       UITableView *tableView = [[UITableView alloc]initWithFrame:self.bounds];
        _tableView = tableView;
        [self addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = YES;
        UIEdgeInsets edgeInset = self.tableView.contentInset;
        edgeInset.top = 13 + 35;
        edgeInset.bottom = 49;
        _tableView.contentInset = edgeInset;
        _tableView.scrollIndicatorInsets =  edgeInset;

    }
    return _tableView;
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return self.sectionGroup.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        NSArray *array = self.sectionGroup[section];
    
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = self.sectionGroup[indexPath.section][indexPath.row];
    
    return cell;
    
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = self.sectionGroup[indexPath.section][indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return [self updateCellHeight:cell];

}

// 设置sectionHeader的高度,如果不设置将不会有高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 13;
}



#pragma mark- 根据cell的类型计算每个cell的高度
- (CGFloat)updateCellHeight:(UITableViewCell *)cell{
    NSString *class = NSStringFromClass(cell.class);

    if ([class isEqualToString:self.titleCell]) {
        return [YCTitleCell getCellHeight];
    }else if([class isEqualToString:self.strategyCell]){
        // 取出相应的cell让cell的代理成为当前控制器
        UITableViewCell *trueCell = cell;
        YCExamStrategyCell *strategyCell = (YCExamStrategyCell *)trueCell;
        strategyCell.delegate = self;
        return [YCExamStrategyCell getCellHeight];
      
    }else if ([class isEqualToString:self.statusCell]){
        return [YCExamStatusCell getCellHeight];
    }else if ([class isEqualToString:self.teachVideoCell]){
        return [YCTeachVideoCell getCellHeight];
    }else{
        return [YCSpecialTrainingCell getCellHeight];
    }
   
}

#pragma mark- YCStrategyCellDelegate
- (void)YCStrategyCelldidSelectItemAtIndexPath:(NSIndexPath *)inidexPath{
        
        UIViewController *vc = self.VCArray[inidexPath.row];
        // 拿到rootView
        UIViewController *rootView = [[UIApplication sharedApplication].keyWindow rootViewController];
        
        // 强制转换在NavControler
        UINavigationController *nav = (UINavigationController *)rootView;
        
        // push到查看更多的view
        [nav pushViewController:vc animated:YES];

}

@end
